package com.unitech.order.controller;

import com.unitech.order.api.beans.ItemInfo;
import com.unitech.order.api.beans.OrderChangeParam;
import com.unitech.order.api.beans.OrderInfo;
import com.unitech.order.api.beans.OrderParam;
import com.unitech.order.service.intf.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /*
    生产新订单号
     */
    @GetMapping("/oid")
    public String getOrderId() {
        return orderService.generateOrderId();
    }

    /*
     下订单
     */
    @PostMapping("/")
    public OrderInfo addOrder(@Valid @RequestBody OrderParam request) {
        log.info("Create order: data={}", request);
        return orderService.createOrder(request);
    }

    /*
    通过订单号查询订单头信息
     */
    @GetMapping("/{id}")
    public OrderInfo loadOrder(@PathVariable String id) {
        return orderService.loadOrder(id);
    }

    /*
    通过订单号查询订单头信息
     */
    @GetMapping("/items/{id}")
    public List<ItemInfo> loadOrderItems(@PathVariable String id) {
        return orderService.loadOrderItems(id);
    }

    /*
    更改订单信息
     */
    @PutMapping("/")
    public OrderInfo changeOrderState(@Valid @RequestBody OrderChangeParam request) {
        return orderService.updateOrderState(request);
    }
}
